package com.gz.logistics.library.util;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.Thread.UncaughtExceptionHandler;


import android.content.Context;
import android.util.Log;

public class CrashHandler implements UncaughtExceptionHandler  {
	
	private static final String TAG = CrashHandler.class.getSimpleName();
	
	/**
	 * 是否开启日志输出,在Debug状态下开启, 在Release状态下关闭以提示程序性能
	 * */
	public static final boolean DEBUG = true;
	/** 系统默认的UncaughtException处理类 */
	private UncaughtExceptionHandler mDefaultHandler;
	
	private static CrashHandler mInstance;
	private Context mContext;

	/** 保证只有一个CrashHandler实例 */
	private CrashHandler(Context context) {
		mContext = context;
		mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
		Thread.setDefaultUncaughtExceptionHandler(this);
	}

	/** 获取CrashHandler实例 ,单例模式 */
	public static CrashHandler getInstance() {
		
		return mInstance;
	}

	/**
	 * 初始化,注册Context对象, 获取系统默认的UncaughtException处理器, 设置该CrashHandler为程序的默认处理器
	 * 
	 * @param context
	 */
	public static void init(Context context) {
		if (mInstance == null) {
			mInstance = new CrashHandler(context);
		}
	}

	/**
	 * 当UncaughtException发生时会转入该函数来处理
	 */
	public void uncaughtException(Thread thread, Throwable ex) {
		if (!handleException(ex) && mDefaultHandler != null) {
			// 如果用户没有处理则让系统默认的异常处理器来处理
			mDefaultHandler.uncaughtException(thread, ex);
		} else {
			if(Tools.hasInternetConnection(mContext)){
//				mContext.startService(new Intent(mContext, ReportService.class));
			}
			// Sleep一会后结束程序
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				Log.e(TAG, "Error : ", e);
			}
			android.os.Process.killProcess(android.os.Process.myPid());
			System.exit(10);
		}
	}

	/**
	 * 自定义错误处理,收集错误信息 发送错误报告等操作均在此完成. 开发者可以根据自己的情况来自定义异常处理逻辑
	 * 
	 * @param e
	 * @return 如果处理了该异常信息返回true, 否则返回false
	 */
	private boolean handleException(Throwable e) {
		if (e == null) {
			return false;
		}
		
		FileWriter writer = null;
		try{
			if(DEBUG){
				e.printStackTrace();
			}
			StringWriter strWriter = new StringWriter();
			e.printStackTrace(new PrintWriter(strWriter));

			String throwableDir = Tools.obtainDirPath(mContext, Constants.FOLDER_REPORT_CACHE);
			writer = new FileWriter(throwableDir + Constants.FILE_ERROR_REPORT, true);
			writer.append(strWriter.toString() + "\n\n\n");
			writer.flush();
			
			return true;
		
		}catch(Exception err){
			CrashHandler.e(err);
		
		}finally {
			try {
				writer.close();
			} catch (IOException e1) {
				CrashHandler.e(e1);
			}
		}
		
		return false;
	}

	public static void d(String tag, String msg){
		if(DEBUG){
			Log.d(tag, msg);
		}
	}
	
	public static void e(String tag, String msg){
		if(DEBUG){
			Log.e(tag, msg);
		}
	}
	
	public static void i(String tag, String msg){
		if(DEBUG){
			Log.i(tag, msg);
		}
	}
	
	public static void w(String tag, String msg){
		if(DEBUG){
			Log.w(tag, msg);
		}
	}
	
	public static void v(String tag, String msg){
		if(DEBUG){
			Log.v(tag, msg);
		}
	}

	public static void e(Throwable e) {
		if(DEBUG){
			e.printStackTrace();
		}
		
	}
}
